package com.azxc.rapid.plus.system.controller;

import com.azxc.rapid.core.boot.ctrl.RapidController;
import com.azxc.rapid.core.secure.utils.AuthUtil;
import com.azxc.rapid.core.tenant.annotation.NonDS;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.plus.system.service.IDeptPlusService;
import com.azxc.rapid.plus.system.vo.TreeNodeVO;
import com.azxc.rapid.system.entity.Dept;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 控制器
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("/systemPlus/deptPlus")
@Api(value = "部门", tags = "部门")
public class DeptPlusController extends RapidController {

	private final IDeptPlusService deptService;


	/**
	 * 获取当前登录人的所属的行政区域，然后行政区域下的部门tree
	 */
	@GetMapping("/treeByUser")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "当前登录人下的部门", notes = "当前登录人下的部门")
	public R<List<TreeNodeVO>> getTreeByUser() {
		List<TreeNodeVO> tree = deptService.getTreeByUser();
		return R.data(tree);
	}

	@GetMapping("/selectDeptByUser")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "根据人员ID查询部门", notes = "传入UserId")
	public R<Dept> selectDeptByUser(@RequestParam Long userId) {
		return R.data(deptService.selectDeptByUser(userId));
	}

	/**
	 * 获取当前登录人的部门tree
	 */
	@GetMapping("/DeptTreeByUser")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "获取当前登录人的部门tree", notes = "获取当前登录人的部门tree")
	public R<List<TreeNodeVO>> DeptTreeByUser() {
		List<TreeNodeVO> tree = deptService.getDeptTreeByUser();
		return R.data(tree);
	}

	/**
	 * DeptTreeByUser接口由于数据太多会慢，需要使用懒加载的方式获取数据
	 * 懒加载的方式获取当前登录人的部门tree
	 */
	@GetMapping("/getDeptLazyTreeByDeptId")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "根据获取部门tree（懒加载）", notes = "根据获取部门tree（懒加载）")
	public R<List<TreeNodeVO>> getDeptLazyTreeByDeptId(String deptId) {
		List<TreeNodeVO> tree = deptService.getDeptLazyTreeByDeptId(deptId);
		return R.data(tree);
	}

	@GetMapping("/ChildDeptByUser")
	@ApiOperationSupport(order = 14)
	@ApiOperation(value = "获取当前登录人所在部门的子部门", notes = "获取当前登录人所在部门的子部门")
	public R<List<TreeNodeVO>> ChildDeptByUser(String deptName) {
		List<TreeNodeVO> tree = deptService.ChildDeptByUser(deptName);
		return R.data(tree);
	}

	@GetMapping("/ChildDeptByUserRegionCode")
	@ApiOperationSupport(order = 15)
	@ApiOperation(value = "获取当前登录人所在部门的子部门", notes = "获取当前登录人所在部门的子部门")
	public R<List<TreeNodeVO>> ChildDeptByUserRegionCode(String deptName) {
		List<TreeNodeVO> tree = deptService.ChildDeptByUser(deptName);
		return R.data(tree);
	}


	/**
	 * 获取当前登录人的部门tree
	 * 如果是管理员则全部查询，非管理员则查询当前部门的子部门
	 * 用于用户创建时进行部门的分配
	 */
	@GetMapping("/selectDeptTreeWithUserGrant")
	@ApiOperationSupport(order = 16)
	@ApiOperation(value = "获取当前登录人的部门tree (依据权限过滤) ", notes = "获取当前登录人的部门tree(依据权限过滤)")
	public R<List<TreeNodeVO>> selectDeptTreeWithUserGrant(String deptId) {
		List<TreeNodeVO> tree = deptService.selectDeptTreeWithUserGrant(deptId);
		return R.data(tree);
	}

	@GetMapping("/selectDeptListByRegion")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "查询当前登录人部门能派发的下级部门 ", notes = "查询当前登录人部门能派发的下级部门(依据权限过滤)")
	public R<List<Dept>> selectDeptListByRegion() {
		Dept dept=	deptService.getById(AuthUtil.getDeptId());
		List<Dept> list = deptService.selectDeptListByRegion(dept.getRegionCode(),AuthUtil.getDeptId());
		return R.data(list);
	}

	/**
	 * 【网格专用】根据行政区划编码查询相关的部门列树形表
	 */
	@GetMapping("/treeByRegion")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "【网格专用】列树形表", notes = "传入行政区划")
	public R<List<TreeNodeVO>> treeByRegion(@RequestParam String regionCode) {
		List<TreeNodeVO> list = deptService.treeByRegion(regionCode);
		return R.data(list);
	}
}
